home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / com_and3.zip / STRIPPER.CMD < prev    next >
OS/2 REXX Batch file  |  1990-07-05  |  7KB  |  242 lines

  1. ; ----- Strip: Massage a COM-AND script
  2. ;
  3. ;    Lines are left justified, and comments are removed.
  4. ;
  5. ;    Warning: If a line length exceeds 80 cols,
  6. ;    no adjustment is made to the line.
  7. ;
  8. ;    R.McG; 4/89, Chicago
  9. ; ----------------------------------------------------------------
  10. ;
  11. ; ----- Ask for an input file name
  12. ;
  13.    CLEAR            ; Clear the screen
  14.    LEGEND " Stripper ver 1.1 "  ; Version #
  15.    WOPEN 10 10 13 70 (blue red) ; Draw a box
  16.    ATSAY 10 12 (blue yellow) " Script stripper "
  17.    ATSAY 11 12 (blue yellow) "Enter the file to be Stripped: "
  18.    ATGET 12 12 (blue white) 58 S0 ; Read file name (58 chars)
  19.    WCLOSE
  20.  
  21.    FOPENI S0 TEXT        ; Try to open file
  22.    IF NOT SUCCESS        ; If file not open
  23.       MESSAGE "*** File not found ***^M^J"
  24.       EXIT            ; Display error message and exit
  25.       ENDIF
  26. ;
  27. ; ----- Ask for the output file
  28. ;
  29. OutputName:
  30.    WOPEN 10 10 13 70 (blue red) ; Draw a box
  31.    ATSAY 10 12 (blue yellow) " Scripted stripper "
  32.    ATSAY 11 12 (blue yellow) "Enter the output file name: "
  33.    ATGET 12 12 (blue white) 58 S0 ; Read file name (58 chars)
  34.    WCLOSE
  35.  
  36.    IF ISFILE S0         ; Test for preexisting file
  37.       WOPEN 10 10 13 70 (blue red) ; Draw a box
  38.       ATSAY 10 12 (blue yellow) " Scripted stripper "
  39.       ATSAY 11 12 (blue yellow) "The output file exists!"
  40.       ATSAY 12 12 (blue yellow) "Do you wish to purge the file? "
  41.       LOCATE 12,68
  42.       KEYGET S1
  43.       WCLOSE
  44.       IF not FIND S1 "Y"
  45.      GOTO OutputName
  46.      ENDIF
  47.       ENDIF
  48. ;
  49. ;    Open the given file
  50. ;
  51.    FOPENO S0 TEXT        ; Try to open file
  52.    IF NOT SUCCESS        ; If file not open
  53.       MESSAGE "*** Error opening output file ***^M^J"
  54.       GOTO OutputName        ; Display error message and exit
  55.       ENDIF
  56. ;
  57. ;    Initialize
  58. ;
  59.    CLEAR            ; Clear the screen (again)
  60.    INIT N0 0            ; Reset line counter
  61.    LEGEND " Script stripper processing"
  62.    SET FLAG(0) OFF        ; Line longer than 80
  63.    GOTO Loop            ; And start
  64. ;
  65. ; ----- End of file
  66. ;
  67. Endfile:
  68.    WRITE "^Z"                   ; Add ASCII file stopper
  69.    FCLOSEO            ; Close output file
  70.    MESSAGE "^M^J*** End of file ***^M^J"
  71.    EXIT             ; And we're done
  72. ;
  73. ; ----- Read a line
  74. ;
  75. Loop:
  76.    READ S0 80 N1        ; Read a line
  77.    IF EOF GOTO EndFile        ; Test for EOF
  78.    IF FLAG(0) GOTO Continue    ; Throw away long line continuations
  79.    IF EQ N1 80 GOTO LongLine
  80.    LJ S0            ; Left justify
  81. ;
  82. ;    Blank lines must thrown away
  83. ;
  84.    IF NULL S0 or ZERO N1    ; Test for a null line
  85.       GOTO Loop         ; .. at top of screen
  86.       ENDIF
  87. ;
  88. ;    Top of forms and other comment lines are thrown away
  89. ;
  90.    IF STRCMP S0(0:0) "^L" or STRCMP S0(0:0) "*" or STRCMP S0(0:0) ";"
  91.       GOTO Loop         ; .. at top of screen
  92.       ENDIF
  93. ;
  94. ;    Skip scan if there is no comment on the line
  95. ;
  96.    IF NOT FIND S0 ";" N1        ; And keep pos of potential comment
  97.       GOTO Finish
  98.       ENDIF
  99.    FIND S0(0:N1) "`"" N2        ; Look for quoted field b4 comment
  100.    IF EQ N2 -1
  101.       S0 = S0(0:N1-1)        ; Truncate right here
  102.       GOTO Finish
  103.       ENDIF
  104.    GOTO Test            ; Test for ';' inside of quotes
  105. ;
  106. ;    Delete trailing blanks
  107. ;
  108. Finish:
  109.    S0 = S0&""
  110. ;
  111. ;    Write the line.  Note the special care taken to see that ^M in the input
  112. ;    .. isn't made into a <cr>, "!" isn't made into a <cr> and so on.
  113. ;
  114. WriteLn:
  115.    LENGTH S0 N1         ; Set new length
  116.    IF GT N1 40
  117.       S1 = S0(0:39)
  118.       S0 = S0(40:N1-1)
  119.       LENGTH S0 N0        ; Send the 1st half
  120.       PRESERVE S1
  121.       LENGTH S1 N1
  122.       WRITE S1 N1
  123.       MESSAGE S1        ; Display line to screen
  124.  
  125.       PRESERVE S0
  126.       LENGTH S0 N0
  127.       WRITE S0 N0
  128.       CURSOR N10,N11        ; Read current cursor
  129.       ATSAY N10,N11 (text) S0    ; Display line to screen
  130.    ELSE
  131.       PRESERVE S0
  132.       LENGTH S0 N0
  133.       WRITE S0 N0
  134.       MESSAGE S0        ; Display line
  135.       ENDIF
  136.    IF NOT FLAG(1)        ; If not a long line, or end of long line
  137.       WRITE "!"                 ; Add a cr/lf
  138.       ENDIF
  139.    GOTO Loop
  140. ;
  141. ; ----- Test for a ':' within a quoted field; N2 = position of 2nd """
  142. ;    Initialize for testing
  143. ;
  144. Test:
  145.    N0 = N2-1            ; -1 as we'll INC first
  146.    LENGTH S0 N1
  147. ;
  148. ;    Scan the line for a comment (allowing for quoted strings)
  149. ;
  150. Test100:
  151.    INC N0
  152.    IF STRCMP S0(N0:N0) "`""
  153.       DO
  154.      INC N0
  155.      IF STRCMP S0(N0:N0) "``"
  156.         N0 = N0+2
  157.         ENDIF
  158.      UNTIL STRCMP S0(N0:N0) "`"" or GE N0 N1
  159.       INC N0
  160.       ENDIF
  161.    IF (NOT STRCMP S0(N0:N0) ";") and LT N0 N1
  162.       GOTO Test100
  163.       ENDIF
  164.    S0 = S0(0:N0-1)
  165.    GOTO Finish
  166. ;
  167. ; ----- A line was read 80 chars long.    Flag (0) is set to provide for
  168. ;    .. continuations of the line.
  169. ;
  170. Continue:
  171.    S1 = "Loop"                  ; Set branch label
  172.    IF FLAG(1) S1 = "FINISH"     ; .. according to flag(1)
  173.    IF LT N1 80            ; If short line
  174.       SET FLAG(0) OFF        ; .. reset ptrs
  175.       SET FLAG(1) OFF
  176.       ENDIF
  177.    GOTO S1            ; Throw away what was read
  178. ;
  179. ; ----- Line read was 80 chars exactly.  Test for a comment in the line
  180. ;    .. anyway.  If found, we'll copy just the instruction and throw
  181. ;    .. away the remainder of the comment.
  182. ;
  183. LongLine:
  184.    IF NULL S0            ; Test for 80 chars of blanks
  185.       GOTO Loop         ; .. at top of screen
  186.       ENDIF
  187.    SET FLAG(0) ON        ; Flag long line condition
  188. ;
  189. ;    Top of forms and other comment lines are thrown away
  190. ;    .. If we find a comment here, FLAG(0) set causes the
  191. ;    .. remainder of the line to be passed to "CONTINUE"
  192. ;
  193.    S1 = S0
  194.    LJ S1            ; Left justify what we have anyway
  195.    LENGTH S1 N1         ; Recompute length
  196.    IF STRCMP S1(0:0) "^L" or STRCMP S1(0:0) "*" or STRCMP S1(0:0) ";"
  197.       GOTO Loop         ; .. at top of screen
  198.       ENDIF
  199. ;
  200. ;    Test for comment on the line
  201. ;
  202.    IF NOT FIND S1 ";" N1        ; And keep pos of potential comment
  203.       GOTO LL200        ; SKip if no possible comment
  204.       ENDIF
  205.    FIND S1(0:N1) "`"" N2        ; Look for quoted field b4 comment
  206.    IF EQ N2 -1
  207.       S0 = S1(0:N1-1)        ; Truncate right here
  208.       GOTO Finish        ; .. and throw away remainder
  209.       ENDIF
  210.    N0 = N2-1
  211. ;
  212. ;    Scan the line for a comment (allowing for quoted strings)
  213. ;
  214. LL100:
  215.    LENGTH S1 N1
  216.    INC N0
  217.    IF STRCMP S1(N0:N0) "`""
  218.       DO
  219.      INC N0
  220.      IF STRCMP S1(N0:N0) "``"
  221.         N0 = N0+2
  222.         ENDIF
  223.      UNTIL STRCMP S1(N0:N0) "`"" or GE N0 N1
  224.       INC N0
  225.       ENDIF
  226.    IF (NOT STRCMP S1(N0:N0) ";") and LT N0 N1
  227.       GOTO LL100
  228.       ENDIF
  229.    IF GE N0 N1            ; If we get to end of line without being
  230.       GOTO LL200        ; .. sure, write the whole line
  231.       ENDIF
  232.    S0 = S1(0:N0-1)        ; We can remove the comment
  233.    GOTO Finish            ; ...
  234. ;
  235. ;    Line contains nothing like a comment
  236. ;
  237. LL200:
  238.    SOUND 440,200
  239.    MESS "^M^J***** Warning - Line greater than 80 *****^M^J"
  240.    SET FLAG(1) ON        ; Indicate a keeper
  241.    GOTO WriteLn         ; Don't delete trailing blanks - may be important
  242.